namespace EFS.Common
{
	public static class Formats
	{
		/// Property DateFormat: Get, Set
		public static System.String DateFormat
		{
			get
			{
				return "dd/MM/yyyy";
			}
		}

		/// <summary>
		/// String to Double
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public static System.Double? ToDouble(System.String value)
		{
			// try parse
			System.Double retValue = 0;
			if (System.Double.TryParse(value, out retValue))
			{
				return retValue;
			}

			// none
			return null;
		}

		/// <summary>
		/// String to Int32
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public static System.Int32? ToInt32(System.String value)
		{
			// try parse
			System.Int32 retValue = 0;
			if (System.Int32.TryParse(value, out retValue))
			{
				return retValue;
			}

			// none
			return null;
		}

		/// <summary>
		/// String to Int64
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public static System.Int64? ToInt64(System.String value)
		{
			// try parse
			System.Int64 retValue = 0;
			if (System.Int64.TryParse(value, out retValue))
			{
				return retValue;
			}

			// none
			return null;
		}

		/// <summary>
		/// String to Single
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public static System.Single? ToSingle(System.String value)
		{
			// try parse
			System.Single retValue = 0;
			if (System.Single.TryParse(value, out retValue))
			{
				return retValue;
			}

			// none
			return null;
		}

		/// <summary>
		/// String to Decimal
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public static System.Decimal? ToDecimal(System.String value)
		{
			// try parse
			System.Decimal retValue = 0;
			if (System.Decimal.TryParse(value, out retValue))
			{
				return retValue;
			}

			// none
			return null;
		}

		public static System.String ExactDoubleTwoDecimal(System.Double value)
		{
			System.String result = "";
			System.String[] help = value.ToString().Split('.');

			if (help.Length > 0)
			{
				result = result + help[0];

				if (help.Length > 1)
				{
					if (help[1].Length > 0)
					{
						result = result + "." + help[1][0];

						if (help[1].Length > 1)
						{
							result = result + help[1][1];
						}
					}
				}
			}

			return result;
		}

		/// <summary>
		/// Format object
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>		
		public static System.String ToString(System.Object value, System.String formatString = null)
		{
			// check null
			if (null == value)
			{
				return null;
			}

			// to string
			if (value is System.DateTime)
			{
				return ((System.DateTime)value).ToString(null == formatString ? DateFormat : formatString);
			}
			else if (value is System.Double)
			{
				return ((System.Double)value).ToString("F");
			}
			else if (value is System.Double?)
			{
				return ((System.Double?)value).Value.ToString("F");
			}
			else if (value is System.Single?)
			{
				return ((System.Single?)value).Value.ToString("F");
			}
			else if (value is System.Decimal)
			{
				return ((System.Decimal)value).ToString();
			}
			else if (value is System.Decimal?)
			{
				return ((System.Decimal?)value).Value.ToString("F");
			}

			// simply to string
			return value.ToString();
		}


		/// Format object as date		
		public static System.String ToStringDate(System.Object value, System.String format)
		{
			// check null
			if (null == value)
			{
				return null;
			}

			// to string
			if (value is System.DateTime)
			{
				return ((System.DateTime)value).ToString(format);
			}

			return null;
		}

		/// <summary>
		/// From string
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public static System.Object FromString(System.String value, System.Type type)
		{
			// check
			if (!EFS.Common.Verify.String(value))
			{
				return null;
			}

			// check type
			if (type == typeof(System.DateTime))
			{
				return System.DateTime.ParseExact(
					value,										
					new System.String[] { "dd/MM/yyyy", "d/M/yyyy", "dd-MM-yyyy", "d-M-yyyy", "dd.MM.yyyy", "d.M.yyyy", "yyyy-MM-dd", "yyyy/MM/dd", "yyyy-MM-dd HH:mm:ss.fff", "dd/MM/yyyy hh:mm:ss", "dd/MM/yyyy hh:mm:ss.fff tt" },
					new System.Globalization.CultureInfo("en-GB"),
					System.Globalization.DateTimeStyles.None);
			}
			else if (type == typeof(System.Guid))
			{
				return new System.Guid((System.String)value);
			}

			// remove digits if int32
			if (type == typeof(System.Int32) && value.Contains(".") )
			{
				value = value.Substring(0, value.IndexOf("."));
			}

			// change type
			return System.Convert.ChangeType(value, type, new System.Globalization.CultureInfo("en-GB"));
		}

		/// Gets the formated price 
		public static System.String GetPercentFormated(System.Single percent)
		{
			System.String sPercent = percent.ToString("F2").Replace(".00", "");
			if (sPercent.IndexOf('.') >= 0)
			{
				sPercent = sPercent.TrimEnd(new char[] { '0' });
			}
			return sPercent;
		}
	}
}